﻿using Microsoft.Extensions.DependencyInjection;
using SimpleORM.Extensions;
using SimpleORM.Infrastructure;
using SimpleORM.Model.DataBaseModel;
using SimpleORM.Repository;
using SimpleORM.Service.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SimpleORM.Service.Service
{
    [Service(ServiceLifetime.Scoped)]
    public class GenerateoneService: BaseRepository<DataBaseModelone>,IGenerateoneService
    {
        public string SYSModelmain_CreateClassFile = AppSettings.GetConfig("SqlSugarConfig:SYSModelmain:CreateClassFile"); 
        public string SYSModelmain_NamespaceNmme = AppSettings.GetConfig("SqlSugarConfig:SYSModelmain:NamespaceNmme"); 


        public bool GenerateDataBaseModel()
        {
            try
            {

                Context.DbFirst
           //类
           .SettingClassTemplate(old => { return old;/*修改old值替换*/ })
           //类构造函数
           .SettingConstructorTemplate(old => { return old;/*修改old值替换*/ })
            .SettingNamespaceTemplate(old =>
            {
                return old + "\r\nusing SqlSugar;"; //追加引用SqlSugar
            })
           //属性备注
           .SettingPropertyDescriptionTemplate(old => { return old;/*修改old值替换*/})

           //属性:新重载 完全自定义用配置
           .SettingPropertyTemplate((columns, temp, type) =>
           {

               var columnattribute = "\r\n           [SugarColumn({0})]";
               List<string> attributes = new List<string>();
               // 如果字段类型是bigint，添加JsonConverter特性
               string jsonConverterAttribute = "";
               if (columns.DataType == "bigint")
               {
                   jsonConverterAttribute = "\r\n           [Newtonsoft.Json.JsonConverter(typeof(ValueToStringConverter))]";
               }
               if (columns.IsPrimarykey)
                   attributes.Add("IsPrimaryKey=true");
               if (columns.IsIdentity)
                   attributes.Add("IsIdentity=true");

               if (attributes.Count == 0)
               {
                   columnattribute = "";
               }
               // 生成SugarColumn特性的字符串
               string sugarColumnAttributes = string.Join(",", attributes);
               string sugarColumnAttribute = string.IsNullOrEmpty(sugarColumnAttributes) ? "" : string.Format(columnattribute, sugarColumnAttributes);

               // 替换模板中的属性定义
               return jsonConverterAttribute + temp.Replace("{PropertyType}", type)
                                                   .Replace("{PropertyName}", columns.DbColumnName)
                                                   .Replace("{SugarColumn}", sugarColumnAttribute);
           })
           .SettingClassDescriptionTemplate(it =>
           {
               return it + "\r\n    [Tenant(\"" + (Context.CurrentConnectionConfig.ConfigId ?? 0).ToString() + "\")]";
           })
           .IsCreateDefaultValue()
           .CreateClassFile(SYSModelmain_CreateClassFile, SYSModelmain_NamespaceNmme);

                return true;
            }
            catch
            (Exception ex)
            {
                return false;
            }
        }

        public bool GenerateListTableModel(string[] Tables)
        {
            var xx = SYSModelmain_CreateClassFile;

            var xx2 = SYSModelmain_CreateClassFile;
            try
            {
                GetModel(Tables);
                return true;
            }
            catch {
                return false;
            }

        }
        void GetModel(string[] Tables)
        {
            for (int i = 0; i < Tables.Length; i++)
            {
                Context.DbFirst
           //类
           .SettingClassTemplate(old => { return old;/*修改old值替换*/ })
           //类构造函数
           .SettingConstructorTemplate(old => { return old;/*修改old值替换*/ })
            .SettingNamespaceTemplate(old =>
            {
                return old + "\r\nusing SqlSugar;"; //追加引用SqlSugar
            })
           //属性备注
           .SettingPropertyDescriptionTemplate(old => { return old;/*修改old值替换*/})

           //属性:新重载 完全自定义用配置
           .SettingPropertyTemplate((columns, temp, type) =>
           {

               var columnattribute = "\r\n           [SugarColumn({0})]";
               List<string> attributes = new List<string>();
               // 如果字段类型是bigint，添加JsonConverter特性
               string jsonConverterAttribute = "";
               if (columns.DataType == "bigint")
               {
                   jsonConverterAttribute = "\r\n           [Newtonsoft.Json.JsonConverter(typeof(ValueToStringConverter))]";
               }
               if (columns.IsPrimarykey)
                   attributes.Add("IsPrimaryKey=true");
               if (columns.IsIdentity)
                   attributes.Add("IsIdentity=true");

               if (attributes.Count == 0)
               {
                   columnattribute = "";
               }
               // 生成SugarColumn特性的字符串
               string sugarColumnAttributes = string.Join(",", attributes);
               string sugarColumnAttribute = string.IsNullOrEmpty(sugarColumnAttributes) ? "" : string.Format(columnattribute, sugarColumnAttributes);

               // 替换模板中的属性定义
               return jsonConverterAttribute + temp.Replace("{PropertyType}", type)
                                                   .Replace("{PropertyName}", columns.DbColumnName)
                                                   .Replace("{SugarColumn}", sugarColumnAttribute);
           })
           .Where($"{Tables[i]}").SettingClassDescriptionTemplate(it =>
           {
               return it + "\r\n    [Tenant(\"" + (Context.CurrentConnectionConfig.ConfigId ?? 0).ToString() + "\")]";
           })
           .IsCreateDefaultValue()
           .CreateClassFile(SYSModelmain_CreateClassFile, SYSModelmain_NamespaceNmme);
            }
        }
    }
    [Service(ServiceLifetime.Scoped)]
    public class GeneratetwoService : BaseRepository<DataBaseModeltwo>, IGeneratetwoService
    {
        public string SYSModelTwo_CreateClassFile = AppSettings.GetConfig("SqlSugarConfig:SYSModelTwo:CreateClassFile");
        public string SYSModelmain_NamespaceNmme = AppSettings.GetConfig("SqlSugarConfig:SYSModelTwo:NamespaceNmme");

        public bool GenerateDataBaseModel()
        {
            try
            {
                Context.DbFirst
           //类
           .SettingClassTemplate(old => { return old;/*修改old值替换*/ })
           //类构造函数
           .SettingConstructorTemplate(old => { return old;/*修改old值替换*/ })
            .SettingNamespaceTemplate(old =>
            {
                return old + "\r\nusing SqlSugar;"; //追加引用SqlSugar
            })
           //属性备注
           .SettingPropertyDescriptionTemplate(old => { return old;/*修改old值替换*/})

           //属性:新重载 完全自定义用配置
           .SettingPropertyTemplate((columns, temp, type) =>
           {

               var columnattribute = "\r\n           [SugarColumn({0})]";
               List<string> attributes = new List<string>();
               // 如果字段类型是bigint，添加JsonConverter特性
               string jsonConverterAttribute = "";
               if (columns.DataType == "bigint")
               {
                   jsonConverterAttribute = "\r\n           [Newtonsoft.Json.JsonConverter(typeof(ValueToStringConverter))]";
               }
               if (columns.IsPrimarykey)
                   attributes.Add("IsPrimaryKey=true");
               if (columns.IsIdentity)
                   attributes.Add("IsIdentity=true");

               if (attributes.Count == 0)
               {
                   columnattribute = "";
               }
               // 生成SugarColumn特性的字符串
               string sugarColumnAttributes = string.Join(",", attributes);
               string sugarColumnAttribute = string.IsNullOrEmpty(sugarColumnAttributes) ? "" : string.Format(columnattribute, sugarColumnAttributes);

               // 替换模板中的属性定义
               return jsonConverterAttribute + temp.Replace("{PropertyType}", type)
                                                   .Replace("{PropertyName}", columns.DbColumnName)
                                                   .Replace("{SugarColumn}", sugarColumnAttribute);
           })
           .SettingClassDescriptionTemplate(it =>
           {
               return it + "\r\n    [Tenant(\"" + (Context.CurrentConnectionConfig.ConfigId ?? 0).ToString() + "\")]";
           })
           .IsCreateDefaultValue()
           .CreateClassFile(SYSModelTwo_CreateClassFile, SYSModelmain_NamespaceNmme);

                return true;
            }
            catch
            (Exception ex)
            {
                return false;
            }
        }

        public bool GenerateListTableModel(string[] Tables)
        {
            try
            {
                GetModel(Tables);
                return true;
            }
            catch
            {
                return false;
            }

        }
        void GetModel(string[] Tables)
        {
            for (int i = 0; i < Tables.Length; i++)
            {
                Context.DbFirst
           //类
           .SettingClassTemplate(old => { return old;/*修改old值替换*/ })
           //类构造函数
           .SettingConstructorTemplate(old => { return old;/*修改old值替换*/ })
            .SettingNamespaceTemplate(old =>
            {
                return old + "\r\nusing SqlSugar;"; //追加引用SqlSugar
            })
           //属性备注
           .SettingPropertyDescriptionTemplate(old => { return old;/*修改old值替换*/})

           //属性:新重载 完全自定义用配置
           .SettingPropertyTemplate((columns, temp, type) =>
           {

               var columnattribute = "\r\n           [SugarColumn({0})]";
               List<string> attributes = new List<string>();
               // 如果字段类型是bigint，添加JsonConverter特性
               string jsonConverterAttribute = "";
               if (columns.DataType == "bigint")
               {
                   jsonConverterAttribute = "\r\n           [Newtonsoft.Json.JsonConverter(typeof(ValueToStringConverter))]";
               }
               if (columns.IsPrimarykey)
                   attributes.Add("IsPrimaryKey=true");
               if (columns.IsIdentity)
                   attributes.Add("IsIdentity=true");

               if (attributes.Count == 0)
               {
                   columnattribute = "";
               }
               // 生成SugarColumn特性的字符串
               string sugarColumnAttributes = string.Join(",", attributes);
               string sugarColumnAttribute = string.IsNullOrEmpty(sugarColumnAttributes) ? "" : string.Format(columnattribute, sugarColumnAttributes);

               // 替换模板中的属性定义
               return jsonConverterAttribute + temp.Replace("{PropertyType}", type)
                                                   .Replace("{PropertyName}", columns.DbColumnName)
                                                   .Replace("{SugarColumn}", sugarColumnAttribute);
           })
           .Where($"{Tables[i]}").SettingClassDescriptionTemplate(it =>
           {
               return it + "\r\n    [Tenant(\"" + (Context.CurrentConnectionConfig.ConfigId ?? 0).ToString() + "\")]";
           })
           .IsCreateDefaultValue()
           .CreateClassFile(SYSModelTwo_CreateClassFile, SYSModelmain_NamespaceNmme);//"D:\\demo\\simpleORMframe\\SimpleORM.Model\\DataBaseModel\\SYSModeltwo");SYSModelTwo
            }
            //"D:\\demo\\simpleORMframe\\SimpleORM\\SimpleORM.Model\\DataBaseModel\\SYSModeltwo"
             //D:\\demo\\simpleORMframe\\SimpleORM.Model\\DataBaseModel\\SYSModelmain\\
        }
    }
}
